package servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import model.Device;
import model.Teacher;

import service.TeacherService;

/**
 * Servlet implementation class TeacherStateServlet
 */
@WebServlet("/TeacherStateServlet")
public class TeacherStateServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    @EJB
    TeacherService teacherService;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public TeacherStateServlet() {

        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String deviceId = request.getParameter("id");
        Device device = new Device(deviceId);

        ArrayList<Teacher> absentTeachers = teacherService.getAbsentTeachersByDevice(device.getDeviceId());
        for (int i = 0; i < absentTeachers.size(); i++) {
            device.addTeacher(absentTeachers.get(i));
        }

        try {

            JAXBContext jc = JAXBContext.newInstance(Device.class);
            Marshaller m = jc.createMarshaller();
            m.marshal(device, response.getOutputStream());
        }
        catch (Exception e) {

            e.printStackTrace();
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}